<template>
  <div class="com-container">
    <div class="com-chart" ref="stock_ref"></div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { getStock } from "@/api";
export default {
  data() {
    return {
      echartsDom: null,
      stockData: null,
      currentIndex: 0,
      timeId: null,
    };
  },
  mounted() {
    this.getData();
    this.initEachat();
    this.screenAdapter();
    this.startIntVal();
    window.addEventListener("resize", this.screenAdapter);
  },
  computed: {
    ...mapState(["style"]),
  },
  watch: {
    style() {
      console.log("主题切换了");
      this.echartsDom.dispose(); // 销毁当前的图表
      this.initEachat(); // 重新以最新的主题名称初始化图表对象
      this.screenAdapter(); // 完成屏幕的适配
      this.upData(); // 更新图表的展示
    },
  },
  destroyed() {
    clearInterval(this.timeId);
  },
  methods: {
    // 获取数据
    async getData() {
      this.stockData = await getStock();
      this.upData();
    },
    // 初始化Eachats元素
    initEachat() {
      this.echartsDom = this.$echarts.init(this.$refs.stock_ref, this.style); //第一个参数是dom元素，第二个是主题
      const initOption = {
        title: {
          text: "▎库存和销量分析",
          left: 20,
          top: 20,
        },
      };
      this.echartsDom.setOption(initOption);
      this.echartsDom.on("mouseover", () => {
        clearInterval(this.timeId);
      });
      this.echartsDom.on("mouseout", () => {
        this.startIntVal();
      });
    },
    // 浏览器大小变化
    screenAdapter() {
      const titleFontSize = (this.$refs.stock_ref.offsetWidth / 100) * 3.6;
      const innerRadius = titleFontSize * 2;
      const outterRadius = innerRadius * 1.125;

      let adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        series: [
          {
            type: "pie",
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2,
            },
          },
          {
            type: "pie",
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2,
            },
          },
          {
            type: "pie",
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2,
            },
          },
          {
            type: "pie",
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2,
            },
          },
          {
            type: "pie",
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2,
            },
          },
        ],
      };
      this.echartsDom.setOption(adapterOption);
      this.echartsDom.resize();
    },
    // 更新页面元素
    upData() {
      const centerArr = [
        ["18%", "40%"],
        ["50%", "40%"],
        ["82%", "40%"],
        ["34%", "75%"],
        ["66%", "75%"],
      ];
      const colorArr = [
        ["#4FF778", "#0BA82C"],
        ["#E5DD45", "#E8B11C"],
        ["#E8821C", "#E55445"],
        ["#5052EE", "#AB6EE5"],
        ["#23E5E5", "#2E72BF"],
      ];

      let LsArr = this.stockData.slice(
        this.currentIndex * 5,
        (this.currentIndex + 1) * 5
      );
      let seriesArr = LsArr.map((item, index) => {
        return {
          hoverAnimation: false, //去除动画效果
          name: item.name + "\n" + item.sales,
          center: centerArr[index],
          type: "pie",
          data: [
            {
              value: item.sales,
              name: item.name + "\n" + item.sales,
              itemStyle: {
                color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: colorArr[index][1] },
                  { offset: 1, color: colorArr[index][0] },
                ]),
              },
            },
            {
              value: item.stock,
              itemStyle: {
                color: "#aaa",
              },
            },
          ],
          label: {
            position: "center",
            color: colorArr[index][0],
          },
          labelLine: {
            show: false,
          },
        };
      });
      const initOption = {
        series: seriesArr,
      };
      this.echartsDom.setOption(initOption);
    },
    startIntVal() {
      if (this.timeId) clearInterval(this.timeId);
      this.timeId = setInterval(() => {
        this.currentIndex++;
        if (this.currentIndex > 1) {
          this.currentIndex = 0;
        }
        this.upData();
      }, 3000);
    },
  },
};
</script>

<style lang="scss" scoped>
</style>